package cf.huzpsb.bcgod;

import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;

import java.util.Iterator;

public class PmCMD extends Command {
    public PmCMD(String name, String permission, String... aliases) {
        super(name, permission, aliases);
    }

    public void execute(CommandSender sender, String[] args) {
        if (!(sender instanceof ProxiedPlayer)) {
            sender.sendMessage("§c后台不能使用此命令!");
        } else if (((ProxiedPlayer) sender).getServer().getInfo().getName().equalsIgnoreCase(Main.login)) {
            sender.sendMessage("§c登录区域禁止使用!");
        } else {
            ProxiedPlayer player = (ProxiedPlayer) sender;
            if (args.length <= 1) {
                player.sendMessage("§c信息和接收人不能为空!");
            } else {
                Data data = Main.getInstance().getAllData().get(player);
                long before = data.time;
                long now = System.currentTimeMillis() / 1000L;
                int time = 3 - (int) (now - before);
                if (time > 0) {
                    player.sendMessage("§c私聊冷却中... " + time + "秒");
                    if (!player.hasPermission("bcgod.bypass")) {
                        return;
                    }

                    player.sendMessage("§a但你绕过了");
                }

                StringBuilder sb = new StringBuilder("§6[");
                sb.append(player.getName());
                sb.append(" -> ");
                sb.append(args[0]);
                sb.append("] §e§f: ");
                String[] var11 = args;
                int var12 = args.length;

                for (int var13 = 1; var13 < var12; ++var13) {
                    String arg = var11[var13];
                    if (sender.hasPermission("bcgod.color")) {
                        arg = arg.replace('&', '§').replace("§§", "&");
                    }

                    sb.append(arg);
                    sb.append(" ");
                }

                Iterator var17 = Main.getInstance().getProxy().getPlayers().iterator();

                String text = sb.toString();

                while (var17.hasNext()) {
                    ProxiedPlayer player1 = (ProxiedPlayer) var17.next();
                    if (player1.hasPermission("bcgod.spy")) {
                        player1.sendMessage(text);
                    }
                }

                var17 = Main.getInstance().getProxy().getPlayers().iterator();

                while (var17.hasNext()) {
                    ProxiedPlayer player1 = (ProxiedPlayer) var17.next();
                    if (player1.getName().equalsIgnoreCase(args[0])) {
                        player.sendMessage(text);
                        player1.sendMessage(text);
                        data.sender = sender.getName();
                        data.time = System.currentTimeMillis() / 1000L;
                        return;
                    }
                }
                player.sendMessage("§c查无此人!");
            }
        }
    }
}
